package com.taotao.proxy.service;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: wangjin
 * @CreateTime: 2022-07-03  22:36
 */
@Slf4j
public class JdkInvocationHandler implements InvocationHandler {
    /**
     * 目标对象
     */
    private Object target;
    public JdkInvocationHandler(Object target){
        this.target=target;
    }
    /**
     *
     * @param proxy jdk自动生成好的代理类
     * @param method 木目标对象的接口
     * @param args 参数类
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      log.info("<jdk动态代理目标方法之前>,args:{}",args);
      Object result=method.invoke(target,args);
        log.info("<jdk动态代理目标方法之后,args:{}", args);
        return result;
    }
    public <T> T getProxy() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
}
